/**
 * @version 1.0
 * @created 03 December 2010
 *
 * ListCategoryItemsController
 *
 * Copyright @ 2010 by CES
 * All Rights Reserved.
 * CES CONFIDENTIAL AND TRADE SECRET
 * 
 */
package com.ces.knacky.controller.tools;

import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ces.knacky.framework.controller.ListController;
import com.ces.knacky.model.Category;
import com.ces.knacky.model.Item;

/**
 * This controller gets the category-wise items.
 * @author shivangi_walvekar
 *
 */
@Controller
public class ListCategoryItemsController extends ListController<Item>{

private String category;

@RequestMapping("/listCategoryItems.htm")
public String execute(){
	System.out.println("calling getCategoryWiseItems : category = "+getCategory());
	listEntity = getCategoryWiseItems();
	return "listCategoryItems";
}

/**
 * This method returns the items for a particular category.
 * 
 * @param category
 * @return
 */
	private List<Item> getCategoryWiseItems() {
		if (null != getCategory() && (!getCategory().isEmpty())) {
			listEntity = (List<Item>) commonService.list("SELECT item from Item item");
			Category category = (Category) commonService.findFirstByDescription
			(Category.class, getCategory());
			if (category != null) {
				listEntity = (List<Item>) commonService.list("SELECT DISTINCT item from Item item," +
				"ItemCategory itemCat where  itemCat.category.id ="+ category.getId()+
				" and itemCat.item.id = item.id");
				System.out.println("<><><><>< size of listEntity in getCategoryWiseItems = "+listEntity.size());
			}
		} else {
			listEntity = (List<Item>) commonService.list("SELECT item from Item item");
		}
		return listEntity;
	}

/**
 * @return category
 */
public String getCategory() {
	return category;
}

/**
 * 
 * @param category
 */
@ModelAttribute("category")
public void setCategory(String category) {
	this.category = category;
}

}
